package com.yaolonga.itfc.manager.core.utils;

import java.util.Map;

/**
 * @Author: yaolong
 * @CreateTime: 2024-02-04  15:10
 * @Description: TODO
 * @Version: 1.0
 */
public class MapUtil {
    public static Object getValueByPath(Map<String, Object> map, String path) {
        String[] keys = path.split("\\.");
        Object value = map;
        for (String key : keys) {
            if (value instanceof Map) {
                value = ((Map<String, Object>) value).get(key);
            } else {
                value = null;
            }
        }
        return value;
    }

    public static <T> T getValueByPath(Map<String, Object> map, String path,Class<T> clazz) {
        if (map == null){
            return null;
        }
        String[] keys = path.split("\\.");
        Object value = map;
        for (String key : keys) {
            if (value instanceof Map) {
                value = ((Map<String, Object>) value).get(key);
            } else {
                value = null;
            }
        }
        try {
            return clazz.cast(value);
        } catch (Exception e) {
            return null;
        }
    }

}
